/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.gshop.domain.po.OrderOpLogsPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.util.Constants.OrderOperate;
import com.itttq.gshop.generic.util.Constants.OrderStatus;
import com.itttq.gshop.pbpm.mapper.IOrderOpLogsMapper;

@Service
public class OrderOpLogsService {
	@Autowired
	private IOrderOpLogsMapper orderOpLogsMapper;
	
	/**
	 * 根据订单ID查询此订单的所有状态变化信息
	 * 
	 * @param orderId	订单ID
	 * @Author 迷蝶
	 * @return List<OrderWarePo>
	 */
	public List<OrderOpLogsPo> getOrderWareByOrderId(int orderId) {
		return orderOpLogsMapper.findOrderOpLogsByOrderId(orderId);
	}
	
	/**
	 * 根据ID获取OrderOpLogsPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return OrderOpLogsPo
	 */
	public OrderOpLogsPo getOrderOpLogs(int id) {
		return orderOpLogsMapper.findOrderOpLogsById(id);
	}
	
	/**
	 * 根据条件获取OrderOpLogsPo对象，并返回查询的数据总数
	 * 
	 * @param orderOpLogs
	 * @Author 迷蝶
	 * @return ResultPageTo<OrderOpLogsPo>
	 */
	public ResultPageTo<OrderOpLogsPo> getOrderOpLogss(OrderOpLogsPo orderOpLogs) {
		return new ResultPageTo<OrderOpLogsPo>(orderOpLogs.getStart(), orderOpLogs.getSize(), 
				orderOpLogsMapper.findOrderOpLogsCount(orderOpLogs), orderOpLogsMapper.findOrderOpLogss(orderOpLogs));
	}
	
	/**
	 * 根据订单ID获取此订单的所有操作日志
	 * 
	 * @param orderOpLogs
	 * @Author 迷蝶
	 * @return ResultPageTo<OrderOpLogsPo>
	 */
	public List<OrderOpLogsPo> getOrderOpLogsByOrderId(int orderId) {
		OrderOpLogsPo orderOpLogs = new OrderOpLogsPo();
		orderOpLogs.setOrderId(orderId);
		orderOpLogs.setSize(999);
		return orderOpLogsMapper.findOrderOpLogss(orderOpLogs);
	}
	
	/**
	 * 根据ID删除OrderOpLogsPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delOrderOpLogs(int id) {
		return orderOpLogsMapper.delOrderOpLogsById(id);
	}
	
	/**
	 * 增加订单日志操作记录
	 * 
	 * @param orderId	订单ID
	 * @param os			订单状态
	 * @param op			操作
	 * @param remark	备注
	 * @param opId		操作人ID
	 * @param opName	操作人用户名或姓名
	 * @Author 迷蝶
	 * @return Integer
	 */
	public Integer addOrderOpLogs(int orderId, OrderStatus os, OrderOperate oo,String remark, Integer opId, String opName) {
		// 修改成功则插入订单状态变化日志记录
		OrderOpLogsPo orderOpLogs = new OrderOpLogsPo();
		orderOpLogs.setOrderId(orderId);
		//TODO GXG 此处应该根据操作有具体详细描述
		orderOpLogs.setDescription(StringUtils.isBlank(remark) ? os.getText() : remark);
		orderOpLogs.setStatus(os.getVal());
		orderOpLogs.setStatusName(os.getText());
		orderOpLogs.setOperation(oo.getVal());
		orderOpLogs.setOperationName(oo.getText());
		orderOpLogs.setOperatorId(opId);
		orderOpLogs.setOperatorName(opName);
		return orderOpLogsMapper.addOrderOpLogs(orderOpLogs);
	}
	
	/**
	 * 
	 * 增加订单日志操作记录
	 * 
	 * @param orderIds订单ID
	 * @param os			订单状态
	 * @param oo			操作枚举对象
	 * @param remark	备注
	 * @param opId		操作人ID
	 * @param opName	操作人用户名或姓名
	 * @return
	 * @Author 迷蝶
	 * @return Integer
	 */
	public Integer addOrderOpLogBatch(List<Integer> orderIds, OrderStatus os, 
			OrderOperate oo,String remark, Integer opId, String opName) {
		Map<String, List<OrderOpLogsPo>> map = new HashMap<String, List<OrderOpLogsPo>>();
		List<OrderOpLogsPo> opLogs = new ArrayList<OrderOpLogsPo>();
		for(Integer orderId : orderIds) {
			// 修改成功则插入订单状态变化日志记录
			OrderOpLogsPo orderOpLogs = new OrderOpLogsPo();
			orderOpLogs.setOrderId(orderId);
			//TODO GXG 此处应该根据操作有具体详细描述
			orderOpLogs.setDescription(StringUtils.isBlank(remark) ? os.getText() : remark);
			orderOpLogs.setStatus(os.getVal());
			orderOpLogs.setStatusName(os.getText());
			orderOpLogs.setOperation(oo.getVal());
			orderOpLogs.setOperationName(oo.getText());
			orderOpLogs.setOperatorId(opId);
			orderOpLogs.setOperatorName(opName);
			opLogs.add(orderOpLogs);
		}
		map.put("opLogs", opLogs);
		return orderOpLogsMapper.insertBatch(map);
	}
	
	/**
	 * 根据条件更新OrderOpLogsPo对象
	 * 
	 * @param orderOpLogs
	 * @Author 迷蝶
	 */
	public Integer updateOrderOpLogs(OrderOpLogsPo orderOpLogs) {
		return orderOpLogsMapper.updateOrderOpLogs(orderOpLogs);
	}
	
}
